package MOLE;

import java.util.*;

public class comm
{
	public comm()
	{
		//
		// TODO: �ڴ˴���ӹ��캯���߼�
		//
	}
	public static String getMid(String s, String startStr, String endStr)
	{
		s = s.toLowerCase();
		startStr = startStr.toLowerCase();
		endStr = endStr.toLowerCase();
		int m = s.indexOf(startStr) + startStr.length();
		int n = s.indexOf(endStr);
		String r = s.substring(m, n).trim();
		return r;
	}


	/** 
	 ��ʾ��Ϣ��ʾ�Ի���
	 
	 @param page ��ǰҳ��ָ�룬һ��Ϊthis
	 @param msg ��ʾ��Ϣ
	*/
	public static void Alert(System.Web.UI.Page page, String msg)
	{


//		page.RegisterStartupScript("message","<script language='javascript' defer>alert('"+msg.ToString()+"');</script>");


	}


	public static void Alert(String msg)
	{
		System.Web.UI.Page p = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;

		String str = "<script language='javascript' defer>" + "if( typeof(CALL)==\"function\" )" + "{ CALL(\"" + msg + "\"); }" + "else{" + "alert('" + msg.toString() + "');" + "}" + "</script>";
	//	p.RegisterStartupScript("message",str);
	}

	public static void AlertMagic(String msg)
	{
	//	MagicAjax.AjaxCallHelper.WriteAlert(msg);
	}


	/** 
	 �ؼ���� ��Ϣȷ����ʾ��
	 
	 @param page ��ǰҳ��ָ�룬һ��Ϊthis
	 @param msg ��ʾ��Ϣ
	*/
	public static void Confirm(System.Web.UI.WebControls.WebControl Control, String msg)
	{
		//Control.Attributes.Add("onClick","if (!window.confirm('"+msg+"')){return false;}");
		Control.Attributes.Add("onclick", "return confirm('" + msg + "');");
	}

	/** 
	 ��ʾ��Ϣ��ʾ�Ի��򣬲�����ҳ����ת
	 
	 @param page ��ǰҳ��ָ�룬һ��Ϊthis
	 @param msg ��ʾ��Ϣ
	 @param url ��ת��Ŀ��URL
	*/
	public static void Alert(System.Web.UI.Page page, String msg, String url)
	{

		//			
		//			StringBuilder Builder=new StringBuilder();
		//			Builder.Append("<script language='javascript' defer>");
		//			Builder.AppendFormat("alert('{0}');",msg);
		//			Builder.AppendFormat("top.location.href='{0}'",url);
		//			Builder.Append("</script>");
		//			page.RegisterStartupScript("message",Builder.ToString());
		//System.Web.HttpContext.Current.
		Alert(msg, url);

	}

	/** 
	 
	 
	 @param msg
	 @param url
	*/
	public static void Alert(String msg, String url)
	{
		System.Web.UI.Page p = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;



		String str = "<script language='javascript' defer>" + "\r\n" + "if( typeof(CALL)==\"function\" )" + "\r\n" + "{ " + "\r\n" + "	CALL(\"" + msg + "\",\"" + url + "\");" + "\r\n" + "}" + "\r\n" + "else" + "\r\n" + "{" + "\r\n" + "	alert('" + msg.toString() + "');" + "\r\n" + "	top.location.href=\"" + url + "\";   " + "\r\n" + "}" + "\r\n" + "</script>" + "\r\n";
		p.RegisterStartupScript("message",str);



		//			System.Web.UI.Page  page=( System.Web.UI.Page )System.Web.HttpContext.Current.Handler;
		//			StringBuilder Builder=new StringBuilder();
		//			Builder.Append("<script language='javascript' defer>");
		//			Builder.AppendFormat("alert('{0}');",msg);
		//			Builder.AppendFormat("top.location.href='{0}'",url);
		//			Builder.Append("</script>");
		//			page.RegisterStartupScript("message",Builder.ToString());
	}
	/** 
	 ����Զ���ű���Ϣ
	 
	 @param page ��ǰҳ��ָ�룬һ��Ϊthis
	 @param script ����ű�
	*/
	public static void RunJS(System.Web.UI.Page page, String script)
	{
		page.RegisterStartupScript("message","<script language='javascript' defer>" + script + "</script>");
	}

	public static void RunJS(String script)
	{
		System.Web.UI.Page p = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
		p.RegisterStartupScript("message" + MOLE.comm.getRandom(),"<script language='javascript' defer>" + script + "</script>");

	}


	public static long DateDiff(DateInterval Interval, java.time.LocalDateTime StartDate, java.time.LocalDateTime EndDate)
	{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#region
		long lngDateDiffValue = 0;
		System.TimeSpan TS = new System.TimeSpan(EndDate.getTime() - StartDate.getTime());
		switch (Interval)
		{
			case Second:
				lngDateDiffValue = (long) TS.TotalSeconds;
				break;
			case Minute:
				lngDateDiffValue = (long) TS.TotalMinutes;
				break;
			case Hour:
				lngDateDiffValue = (long)TS.TotalHours;
				break;
			case Day:
				lngDateDiffValue = (long) TS.Days;
				break;
			case Week:
				lngDateDiffValue = (long)(TS.Days / 7);
				break;
			case Month:
				lngDateDiffValue = (long)(TS.Days / 30);
				break;
			case Quarter:
				lngDateDiffValue = (long)((TS.Days / 30) / 3);
				break;
			case Year:
				lngDateDiffValue = (long)(TS.Days / 365);
				break;
		}
		return (lngDateDiffValue);
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#endregion
	} //end of DateDiff


	public static String DatetimeFormat(String s)
	{
		//string s="20070228105438";
		//DateTime d=DateTime.Parse(s);
		String d = s.substring(0,8);
		String year = s.substring(0,4);
		String month = s.substring(4, 6);
		String day = s.substring(6, 8);
		d = year + "-" + month + "-" + day;
		String h = s.substring(8, 10);
		String m = s.substring(10, 12);
		String sec = s.substring(12);
		String time = h + ":" + m + ":" + sec;
		String now = d + "��" + time;
		//			Response.Write(now);
		return now;

	}


	public static int Text_Length(String Text)
	{
		int len = 0;

		for (int i = 0;i < Text.length();i++)
		{
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] byte_len =System.Text.Encoding.Default.GetBytes(Text.Substring(i,1));
			byte[] byte_len = System.Text.Encoding.Default.GetBytes(Text.substring(i, i + 1));
			if (byte_len.length > 1)
			{
				len += 2; //������ȴ���1�������ģ�ռ�����ֽڣ�+2
			}
			else
			{
				len += 1; //������ȵ���1����Ӣ�ģ�ռһ���ֽڣ�+1
			}
		}

		return len;
		//	System.Text.Encoding.Default.g
	}

	public static boolean IsHasJapanese(String Text)
	{

		boolean ishav = false;
		String s;
		for (int i = 0;i < Text.length();i++)
		{
			s = (Text.substring(i, i + 1));
			if (Asc(s) > 255)
			{
				ishav = true;
				return ishav;

			}


		}
		return ishav;

	}


	public static boolean IsAllJapanese(String Text)
	{

		boolean ishav = false;
		String s;
		for (int i = 0;i < Text.length();i++)
		{
			s = (Text.substring(i, i + 1));
			if (Asc(s) < 255)
			{
				ishav = true;
				return !ishav;

			}


		}
		return !ishav;

	}
	public static String Sec2Time(int secs)
	{
		int sec = secs % 60;
		int m = secs / 60;
		return MOLE.str.ToTimeFormat(m) + ":" + MOLE.str.ToTimeFormat(sec);
	}
	public static int Text_Length2(String Text)
	{
		int len = 0;
		String s = "";


		for (int i = 0;i < Text.length();i++)
		{
			s = (Text.substring(i, i + 1));
			if (Asc(s) > 255)
			{
				len += 2; //������ȴ���1�������ģ�ռ�����ֽڣ�+2
			}
			else
			{
				len += 1; //������ȵ���1����Ӣ�ģ�ռһ���ֽڣ�+1
			}
		}

		return len;
		//	System.Text.Encoding.Default.g
	}

	public static int Asc(String s)
	{
		int i = (int)(s.toCharArray()[0]);
		return i;
	}
	public static String Asc2Str(String s)
	{
		//int i=Convert.ToInt32(s.ToCharArray()[0]);
		String ss;
		ss = String.valueOf(s);
		return s;
	}


	public static String getDateTime()
	{

		String out_trade_no = getDate() + getTime();

		return out_trade_no;
	}
	public static String getRandom()
	{
		return getDateTime() + getRandom(4);
	}

	public static String getRandom(int length)
	{
		Random random = new Random();
		String s = random.nextInt().toString();
		s = s.substring(0,length);
		return s;
	}

	public static String getRandomChars(int length)
	{
		String a = getRandom(8);
		String b = getRandom(8);
		String c = getRandom(8);
		String r = EncryptDecrypt.TMD5(a + b + c);
		return r;
	}


	public static String getDate()
	{
		java.time.LocalDateTime d = java.time.LocalDateTime.now();
		String y = getDouble(d.getYear());
		String m = getDouble(d.getMonthValue());
		String day = getDouble(d.getDayOfMonth());
		String s = y + m + day;
		return s;
	}
	public static String getTime()
	{
		java.time.LocalDateTime d = java.time.LocalDateTime.now();
		String y = getDouble(d.getHour());
		String m = getDouble(d.getMinute());
		String day = getDouble(d.getSecond());
		String s = y + m + day;
		return s;

	}
	public static String getDouble(int i)
	{
		return (i < 10)?"0" + String.valueOf(i):String.valueOf(i);
	}

	public static Hashtable HabinHashtable(Hashtable h1, Hashtable h2)
	{
		ArrayList a = new ArrayList();
		a.add(h1);
		a.add(h2);
		return HabinHashtable(a);
	}

	public static Hashtable HabinHashtable(ArrayList hashtableList)
	{
		Hashtable NewHashtable = new Hashtable();

		for (int i = 0;i < hashtableList.size();i++)
		{
			Hashtable tmp = (Hashtable)hashtableList.get(i);
			for (DictionaryEntry myDE : tmp)
			{
				Object cmdText = myDE.Key;
				Object cmdParms = myDE.Value;
				NewHashtable.put(cmdText,cmdParms);
			}
		}

		return NewHashtable;


	}

	public static ArrayList HabinArraylist(ArrayList h1, ArrayList h2)
	{
		ArrayList a = new ArrayList();
		for (int i = 0;i < h1.size();i++)
		{
			a.add(h1.get(i));
		}
		for (int i = 0;i < h2.size();i++)
		{
			a.add(h2.get(i));
		}
		return a;
	}

	public static ArrayList HabinArraylist(ArrayList h1, ArrayList h2, ArrayList h3)
	{
		ArrayList a = HabinArraylist(h1, h2);

		ArrayList c = HabinArraylist(a, h3);
		return c;

	}


	public static DataTable HabinDatatable(DataTable a, DataTable b)
	{

		for (int i = 0; i < b.Rows.size(); i++)
		{
			a.Rows.Add(b.Rows[i].ItemArray);
		}

		DataTable h = a;

		return h;
	}

	public static ArrayList NoRepeat(ArrayList a)
	{
		ArrayList aa = new ArrayList();
		for (Object i : a)
		{
			if (IsContain(aa, i))
			{
			}
			else
			{
				aa.add(i);
			}
		}
		return aa;
	}

	public static boolean IsContain(ArrayList a, Object o)
	{
		for (Object i : a)
		{
			if (o == i)
			{
				return true;
			}
		}
		return false;
	}


	//		public static string ConvertNoneToDoublueLine( s)
	//		{
	//
	//		}
	public static String ConvertNoneToDoublueLine(Object s)
	{
		double d = str.ConvertToDouble(s.toString());
		if (d == 0)
		{
			return "--";
		}
		else
		{
			return String.format("%0.2f", d);
		}

	}

	public static String AddPreZero(Object o, int length)
	{
		//string tickId=tickIdstr.ToString();
		String s = "";
		for (int i = 0;i < (length - o.toString().length());i++)
		{
			s += "0";
		}
		String r = s + o.toString();
		return r;

	}

	public static String SIDMask(String s, int pre, int last)
	{
		String t = s;
		if (pre + last > s.length())
		{
			pre = 1;
			last = 1;
			if (s.length() <= 2)
			{
				return s;
			}
		}

		String p = s.substring(0,pre);
		String m = s.substring(pre, s.length() - last);
		m = AddPreZero("", m.length());
		m = m.replace("0","*");
		String l = s.substring(s.length() - last);

		t = p + m + l;
		return t;
	}

	public static String MoneyMask(Object money)
	{
		java.math.BigDecimal m = new java.math.BigDecimal(0);
		try
		{
			m = (java.math.BigDecimal)money;
		}
		catch (java.lang.Exception e)
		{
		}

		if (m.compareTo(0) == 0)
		{
			return "--";
		}
		else
		{
			return m.toString("0.00");
		}

	}

	public static int SubstrTimes(String str, String str_mini)
	{

		int i = str.length() - str.replace(str_mini, "").length();
		return i;
	}

	public static int ArrMax(ArrayList a)
	{


		int max = Integer.parseInt(a.get(0).toString());
		for (int i = 1; i < a.size(); i++)
		{
			if (Integer.parseInt(a.get(0).toString()) > max)
			{
				max = Integer.parseInt(a.get(0).toString());
			}
		}
		return max;


	}

	public static int ArrMini(ArrayList a)
	{


		int max = Integer.parseInt(a.get(0).toString());
		for (int i = 1; i < a.size(); i++)
		{
			if (Integer.parseInt(a.get(0).toString()) > max)
			{
				max = Integer.parseInt(a.get(0).toString());
			}
		}
		return max;


	}




	//   This   example   demonstrates   reading   a   property   of   a   ManagementObject.   

	public static String GetFreeSpace()
	{
		ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"d:\"");
		disk.Get();
		String s = ("Logical   Disk   Size   =   " + disk.getItem("Size") + "   bytes");

		//Console.ReadLine();     
		return s;
	}


//		static string GetFreeSpace(string driveDirectoryName)
//		{
//				
//		      DirectoryInfo o=new 	System.IO.DirectoryInfo("d:\\");
//		   return    o
//		}

//		/// <summary>
//		/// ȡ�ô���ʣ��ռ�
//		/// </summary>
//		/// <param name="driveDirectoryName">��������</param>
//		/// <returns>ʣ��ռ�</returns>
//		private static long GetFreeSpace(string driveDirectoryName)
//		{
//			long freefreeBytesAvailable = 0;
//
//			DriveInfo drive = new DriveInfo(driveDirectoryName);
//
//		 
//
//			freefreeBytesAvailable = (long)drive.AvailableFreeSpace;
//
//			return freefreeBytesAvailable;
//		}

//		[DllImport("kernel32.dll")]
//		private static extern bool GetDiskFreeSpaceEx(
//			string lpDirectoryName, out ulong lpFreeBytesAvailable, out ulong lpTotalNumberOfBytes, out ulong lpTotalNumberOfFreeBytes);
//
//		/// <summary>
//		/// ȡ�ô���ʣ��ռ�
//		/// </summary>
//		/// <param name="driveDirectoryName">��������</param>
//		/// <returns>ʣ��ռ�</returns>
//		private static ulong GetFreeSpace(string driveDirectoryName)
//		{
//			ulong freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes;
//			if (!driveDirectoryName.EndsWith(":\\"))
//			{
//				driveDirectoryName += ":\\";
//			}
//			GetDiskFreeSpaceEx(driveDirectoryName, out freeBytesAvailable, out totalNumberOfBytes, out totalNumberOfFreeBytes);
//			return freeBytesAvailable;
//		}

//		//��ȡ����ʣ��ռ�;
//		[DllImport( "kernel32.dll", EntryPoint="GetDiskFreeSpaceA" )]
//		public static extern int GetDiskFreeSpace(string lpRootPathName,ref int lpSectorsPerCluster,
//			ref int lpBytesPerSector,
//			ref int lpNumberOfFreeClusters,
//			ref int lpTotalNumberOfClusters);
//
//		//��ȡ��������;
//		[DllImport( "kernel32.dll", EntryPoint="GetDriveTypeA" )]
//		public static extern int GetDriveType(string nDrive);






}